<?php
/* @file edit-problem.php
 * @author Liuexp
 * @description php interface for editing problem
 */


require_once("helper.php");
if(!InGroup(GROUP_EDIT_PROBLEM)){
	MsgBox(ACCESS_DENIED);
	exit();
}
if($_GET['op']=='submit')
{
	$volume_name=$_GET['volume'];
	$name=$_GET['problem'];
	$bean=R::findOne("problem","name='$name' AND volume_name='$volume_name'");
	$bean->import($_POST,"name,sharetronix_user_id,validator_name,visible,enabled");
	$data=array('title'=>$_POST['title'],'description'=>$_POST['description']);
	foreach($_POST as $key => $val){
		if(strpos($key,'testcase_')!==false)
			$testcase[$key]=$val;
	}
	$data['testcase']=$testcase;
	//TODO:verify the validity of $data
	$bean->data=serialize($data);
	$bean->volume_name=$volume_name;
	if(!isValidProblem($bean))MsgBox(INVALID_PROBLEM);
	else {
		try{
			R::store($bean);
		}
		catch(Exception $e)
		{
			//TODO
		}
	}
}
else {
	try{

		$dwoo=new Dwoo();
		$tpl=new Dwoo_Template_File('template/edit-problem.tpl');
		$data=new Dwoo_Data();

		$volume_name=$_GET['volume'];
		$name=$_GET['problem'];
		$bean=R::findAndExport("problem","name='$name' AND volume_name='$volume_name'");
		$arr=reset($bean);
		$arr->data=unserialize($arr->data);
		$$data->assign('sharetronix_user_id',GetUserID());
		$data->assign('problem',$arr);
		$dwoo->output($tpl,$data);
	}
	catch(Exception $e)
	{
		OJLog("Error in edit-problem.php invoking Dwoo:". $e->getMessage());
	}
}
?>
